package com.ycm.core;

import lombok.Data;

import java.io.Serializable;

/**
 * WebApi标准返回结构
 *
 * @author ycm
 * @date 2024-11-13
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private String code;
    /**
     * 返回内容
     */
    private String msg;
    /**
     * 数据对象
     */
    private T data;
    /**
     * 错误信息
     */
    private String errors;

    private Long timestamp = System.currentTimeMillis();

    public static <T> Result<T> result(String code, String msg, T data, String errors) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        result.setErrors(errors);
        return result;
    }

    public static <T> Result<T> success() {
        return result(ResultCode.SUCCESS, "成功", null, null);
    }

    public static <T> Result<T> success(T data) {
        return result(ResultCode.SUCCESS, "成功", data, null);
    }

    public static <T> Result<T> success(T data, String msg) {
        return result(ResultCode.SUCCESS, msg, data, null);
    }

    public static <T> Result<T> fail(String code, String msg) {
        return result(code, msg, null, null);
    }
}
